import 'dart:convert';
import 'dart:math';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:dio/dio.dart';
import 'package:flutter/services.dart';
import 'package:flutter_app/movie/Movie_detials.dart';
class Popular_list extends StatefulWidget {
  Popular_listWidget createState() => new Popular_listWidget();
}

class Popular_listWidget extends State<Popular_list> {
  List _results;
  Widget _body;
  int page = 1;
  TextEditingController textEditingController= new TextEditingController();
  String errortext=null;
  getDate() async {
    Dio dio = new Dio();
    Response response = await dio.get(
        "https://api.themoviedb.org/3/movie/popular?api_key=80ac1d0d481f2a1e3e51b5cf5703cf8b&language=zh-CN&page=$page");
    var _data = JsonDecoder().convert(response.toString());
    _results = _data["results"];
    print(_results[0]["poster_path"]);
    setState(() {
      _body = Container(
        child: Column(
          children: <Widget>[
            Expanded(
              flex: 12,
              child: ListView.separated(
                  separatorBuilder: (context, index) {
                    return Divider(
                      color: Colors.black,
                    );
                  },
                  itemCount: 20,
                  itemBuilder: (BuildContext context, int position) {
                    return Container(
                      child: GestureDetector(
                        onTap: (){
                          Navigator.push(context, new MaterialPageRoute(builder: (context)=>detials(_results[position]["id"])));
                        },
                        child: Row(
                          children: <Widget>[
                            Expanded(
                                flex: 4,
                                child: Container(
                                  height: 200,
                                  child: new FadeInImage(
                                      fit: BoxFit.cover,
                                      placeholder:
                                      AssetImage('assets/loading.gif'),
                                      image: NetworkImage(
                                          "https://image.tmdb.org/t/p/w500" +
                                              _results[position]["poster_path"])),
                                )),
                            Expanded(
                              flex: 7,
                              child: Container(
                                height: 200,
                                child: ConstrainedBox(
                                  constraints: BoxConstraints(
                                    minHeight: 200,
                                    minWidth: double.infinity,
                                  ),
                                  child: Column(
                                    mainAxisAlignment: MainAxisAlignment.start,
                                    crossAxisAlignment: CrossAxisAlignment.start,
                                    children: <Widget>[
                                      Container(
                                        padding:
                                        EdgeInsets.fromLTRB(5.0, 0, 5.0, 0),
                                        child: Center(
                                          child: Text(
                                            _results[position]["title"] +
                                                " (" +
                                                _results[position]
                                                ["original_title"] +
                                                ")",
                                            style: TextStyle(
                                              fontSize: 14.0,
                                            ),
                                          ),
                                        ),
                                      ),
                                      Container(
                                        child: Center(
                                          child: Text(
                                            _results[position]["release_date"],
                                            style: TextStyle(fontSize: 10.0),
                                          ),
                                        ),
                                      ),
                                      Container(
                                        padding:
                                        EdgeInsets.fromLTRB(5.0, 2.0, 5.0, 0),
                                        child: Text(
                                          _results[position]["overview"],
                                          overflow: TextOverflow.ellipsis,
                                          maxLines: 6,
                                        ),
                                      ),
                                    ],
                                  ),
                                ),
                              ),
                            )
                          ],
                        ),
                      ),
                    );
                  }),
            ),
            Expanded(
              flex: 1,
              child: Container(
                child: Stack(
                  alignment: Alignment.center,
                  children: <Widget>[
                    Positioned(
                      left: 5.0,
                      child: RaisedButton(
                        onPressed: () {
                          page--;
                        },
                        child: Icon(Icons.arrow_left),
                      ),
                    ),
                    Positioned(
                      right: 5.0,
                      child: RaisedButton(
                        onPressed: () {
                          setState(() {
                            page++;
                          });
                        },
                        child: Icon(Icons.arrow_right),
                      ),
                    ),
                    Positioned(
                      left: 140.0,
                      child: Container(
                        child: Row(
                          children: <Widget>[
                            SizedBox(
                              width: 100.0,
                              child: Container(
                                padding: EdgeInsets.fromLTRB(0, 10.0, 0, 10.0),
                                child: Container(
                                  child: Row(
                                    children: <Widget>[
                                      SizedBox(
                                        width: 35.0,
                                        height: 30.0,
                                        child: Container(
                                          padding: EdgeInsets.only(top: 5.0),
                                          child: GestureDetector(
                                            onTap: (){
                                              showCupertinoDialog(
                                                  context: context,
                                                  builder: (context) {
                                                    errortext=null;
                                                    textEditingController.text="$page";
                                                    return CupertinoAlertDialog(
                                                      title: Text('页数'),
                                                      content: Card(
                                                        elevation: 0.0,
                                                        child: Column(
                                                          children: <Widget>[
                                                            TextField(
                                                              inputFormatters:[WhitelistingTextInputFormatter.digitsOnly] ,
                                                              controller:textEditingController,
                                                              onChanged: (text){
                                                                page=int.parse(text);
                                                              },
                                                              keyboardType: TextInputType.numberWithOptions(),
                                                              decoration: InputDecoration(
                                                                errorText: errortext,
                                                                  hintText: '输入想去的页数',
                                                            ),)
                                                          ],
                                                        ),
                                                      ),
                                                      actions: <Widget>[
                                                        CupertinoDialogAction(
                                                          onPressed: () {
                                                            Navigator.pop(context);
                                                          },
                                                          child: Text('取消'),
                                                        ),
                                                        CupertinoDialogAction(
                                                          onPressed: () {
                                                            if(int.parse(textEditingController.text)>0&&int.parse(textEditingController.text)<=500){
                                                              setState(() {
                                                                page=int.parse(textEditingController.text);
                                                                Navigator.pop(context);
                                                              });
                                                            }
                                                            else{
                                                              setState(() {
                                                                textEditingController.text="";
                                                                errortext="请输入正确的页数";
                                                              });
                                                            }
                                                          },
                                                          child: Text('确定'),
                                                        ),
                                                      ],
                                                    );
                                                  });
                                            },
                                            child: Center(
                                              child: Text('$page'),
                                            ),
                                          ),
                                          decoration: BoxDecoration(
                                            border: Border.all(width: 2.0),
                                            borderRadius: BorderRadius.all(Radius.circular(5.0))
                                          ),
                                        )
                                      ),
                                      SizedBox(
                                        width: 50.0,
                                          height: 30.0,
                                        child: Container(
                                          padding: EdgeInsets.only(left: 5.0),
                                          child:Center(
                                            child:  Text('/ 500',style: TextStyle(
                                              fontSize: 15.0
                                            ),),
                                          ),
                                        ),
                                      )
                                    ],
                                  ),
                                ),
                              ),
                            )
                          ],
                        ),
                      ),
                    )
                  ],
                ),
              ),
            )
          ],
        ),
      );
    });
  }

  void initState() {
    _body =Column(
      children: <Widget>[
       Expanded(
         flex: 12,
         child: Container(
           child: Center(
             child:  CircularProgressIndicator(),
           ),
         ),
       ),
        Expanded(
          flex: 1,
       child:  Container(
         child: Stack(
           alignment: Alignment.center,
           children: <Widget>[
             Positioned(
               left: 5.0,
               child: RaisedButton(
                 onPressed: () {
                   page--;
                 },
                 child: Icon(Icons.arrow_left),
               ),
             ),
             Positioned(
               right: 5.0,
               child: RaisedButton(
                 onPressed: () {
                   setState(() {
                     page++;
                   });
                 },
                 child: Icon(Icons.arrow_right),
               ),
             ),
             Positioned(
               left: 140.0,
               child: Container(
                 child: Row(
                   children: <Widget>[
                     SizedBox(
                       width: 100.0,
                       child: Container(
                         padding: EdgeInsets.fromLTRB(0, 10.0, 0, 10.0),
                         child: Container(
                           child: Row(
                             children: <Widget>[
                               SizedBox(
                                   width: 35.0,
                                   height: 30.0,
                                   child: Container(
                                     padding: EdgeInsets.only(top: 5.0),
                                     child: GestureDetector(
                                       onTap: (){
                                         showCupertinoDialog(
                                             context: context,
                                             builder: (context) {
                                               errortext=null;
                                               textEditingController.text="$page";
                                               return CupertinoAlertDialog(
                                                 title: Text('页数'),
                                                 content: Card(
                                                   elevation: 0.0,
                                                   child: Column(
                                                     children: <Widget>[
                                                       TextField(
                                                         inputFormatters:[WhitelistingTextInputFormatter.digitsOnly] ,
                                                         controller:textEditingController,
                                                         onChanged: (text){
                                                           page=int.parse(text);
                                                         },
                                                         keyboardType: TextInputType.numberWithOptions(),
                                                         decoration: InputDecoration(
                                                           errorText: errortext,
                                                           hintText: '输入想去的页数',
                                                         ),)
                                                     ],
                                                   ),
                                                 ),
                                                 actions: <Widget>[
                                                   CupertinoDialogAction(
                                                     onPressed: () {
                                                       Navigator.pop(context);
                                                     },
                                                     child: Text('取消'),
                                                   ),
                                                   CupertinoDialogAction(
                                                     onPressed: () {
                                                       if(int.parse(textEditingController.text)>0&&int.parse(textEditingController.text)<=500){
                                                         setState(() {
                                                           page=int.parse(textEditingController.text);
                                                           Navigator.pop(context);
                                                         });
                                                       }
                                                       else{
                                                         setState(() {
                                                           textEditingController.text="";
                                                           errortext="请输入正确的页数";
                                                         });
                                                       }
                                                     },
                                                     child: Text('确定'),
                                                   ),
                                                 ],
                                               );
                                             });
                                       },
                                       child: Center(
                                         child: Text('$page'),
                                       ),
                                     ),
                                     decoration: BoxDecoration(
                                         border: Border.all(width: 2.0),
                                         borderRadius: BorderRadius.all(Radius.circular(5.0))
                                     ),
                                   )
                               ),
                               SizedBox(
                                 width: 50.0,
                                 height: 30.0,
                                 child: Container(
                                   padding: EdgeInsets.only(left: 5.0),
                                   child:Center(
                                     child:  Text('/ 500',style: TextStyle(
                                         fontSize: 15.0
                                     ),),
                                   ),
                                 ),
                               )
                             ],
                           ),
                         ),
                       ),
                     )
                   ],
                 ),
               ),
             )
           ],
         ),
       ),
        )
      ],
    );
    getDate();
  }

  Widget build(BuildContext context) {
    getDate();
    return Scaffold(
      appBar: AppBar(
        title: Text('热门影片'),
        centerTitle: true,
      ),
      body: _body,
    );
  }
}
